/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "console_win.h"

#include <cstdarg>
#include <cstdio>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

namespace node {
    namespace sys {

        Console::Console(void)
        {

        }

        Console::~Console(void)
        {

        }

        void Console::Info(const char *format, ...)
        {
            ::std::va_list args;
            va_start(args, format);
            ::vfprintf_s(stdout, format, args);
            va_end(args);
        }

        void Console::Log(const char *format, ...)
        {
            ::std::va_list args;
            va_start(args, format);
            ::vfprintf_s(stdout, format, args);
            va_end(args);
        }

        void Console::Warning(const char *format, ...)
        {
            HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
            if (hStdOut != INVALID_HANDLE_VALUE && hStdOut != NULL)
            {
                SetConsoleTextAttribute(hStdOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
                ::std::va_list args;
                va_start(args, format);
                ::vfprintf_s(stderr, format, args);
                va_end(args);
                SetConsoleTextAttribute(hStdOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
            }
            else
            {
                ::std::va_list args;
                va_start(args, format);
                ::vfprintf_s(stderr, format, args);
                va_end(args);
            }
        }

        void Console::Error(const char *format, ...)
        {
            HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
            if (hStdOut != INVALID_HANDLE_VALUE && hStdOut != NULL)
            {
                SetConsoleTextAttribute(hStdOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
                ::std::va_list args;
                va_start(args, format);
                ::vfprintf_s(stderr, format, args);
                va_end(args);
                SetConsoleTextAttribute(hStdOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
            }
            else
            {
                ::std::va_list args;
                va_start(args, format);
                ::vfprintf_s(stderr, format, args);
                va_end(args);
            }
        }

    }
}